package cn.gjculture.shopping.api.controller;

import cn.gjculture.shopping.api.entity.PageInfo;
import cn.gjculture.shopping.api.entity.Return;
import cn.gjculture.shopping.api.entity.User;
import cn.gjculture.shopping.api.service.UserService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping(value = "/user")
public class UserController {

    @Autowired
    UserService userService;

    @ApiOperation(value = "新增用户", notes = "新增用户")
    @ApiImplicitParam(name = "user", value = "用户对象", required = true, dataType = "Object")
    @PostMapping
    public Return save(User user) {
        Return ret = new Return();
        if(userService.insert(user)>0){
            ret.getCommon().setRet(true);
        }
        return ret;

    }
    @ApiOperation(value = "修改用户", notes = "修改用户")
    @ApiImplicitParam(name = "user", value = "用户对象", required = true, dataType = "Object")
    @PutMapping
    public Return<User> update(User user){
        Return<User> ret=new Return<>();
        if(userService.update(user)>0){
            ret.getCommon().setRet(true);
        }
        return ret;
    }

    @ApiOperation(value = "获取用户列表", notes = "获取用户列表")
    @GetMapping
    public Return<List<User>> query(PageInfo pageInfo) {
        Return<List<User>> ret = new Return<>();
        List<User> users = userService.query();
        ret.setData(users);
        return ret;
    }

    @ApiOperation(value = "获取用户", notes = "获取用户")
    @ApiImplicitParam(name = "userId", value = "用户ID", required = true, dataType = "String",paramType = "path")
    @GetMapping(value = "/{userId}")
    public Return<User> gitActivity(@PathVariable(value = "userId") String userId) {
        Return<User> ret = new Return<>();
        User user = userService.queryById(userId);
        ret.setData(user);
        return ret;
    }


    @ApiOperation(value = "删除用户", notes = "删除用户")
    @ApiImplicitParam(name = "userIds", value = "用户ID", required = true, dataType = "String",paramType = "path")
    @DeleteMapping(value = "/{userIds}")
    public Return<User> delete(@PathVariable(value = "userIds") List<String> userIds) {
        Return<User> ret = new Return<>();
        if(userService.delete(userIds)>0){
            ret.getCommon().setRet(true);
        }
        return ret;
    }

    @PutMapping(value = "/password")
    public Return<User> updatePassword(@RequestHeader(value="token") String tokenId, String oldPassword,String newPassword) {
        Return<User> ret = new Return<>();
        if(userService.updatePassword(tokenId,oldPassword,newPassword)){
            ret.getCommon().setRet(true);
        }
        return ret;
    }
}
